// pages/home/home.js
var util = require ('../../utils/util.js');
Page({

    /**
     * 页面的初始数据
     */
    data: {
      isSuccess:true,
      baseURL: 'http://43.139.97.111:8080',
      page:1,
      books:[],
      banners:[
        {
          id:1,
          imageurl:"https://img0.baidu.com/it/u=3443589548,2551169915&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=344"
        },
        {
          id:2,
          imageurl:"https://img0.baidu.com/it/u=3199633389,2539850990&fm=253&fmt=auto&app=138&f=JPEG?w=640&h=440"
        },
        {
          id:3,
          imageurl:"https://img0.baidu.com/it/u=3374260831,2151402371&fm=253&fmt=auto?w=640&h=440"
        }
      ]
    },
    

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
      var _this=this;
      _this.getRmbooks();
      _this.getNewbooks();
      _this.getAllbooks();
    },

    getRmbooks: function () {
      var _this=this;
      let page = this.data.page;
      wx.showLoading({
        title: '加载中',
        mask:true
      })
      wx.request({
          url: 'http://43.139.97.111:8080/rmBooks/queryByPage?pageNum=' + page + '&pageSize=4',       
          header: {
            'content-type': 'application/json' // 默认值
          },
          success (res) {
            console.log(res.data);
            if(res.statusCode==200){
              _this.setData({
                rmbooks:res.data.data.list,
              });
            }
          wx.hideLoading()
        }
      })
    },

    getNewbooks: function () {
      var _this=this;
      let page = this.data.page;
      wx.showLoading({
        title: '加载中',
        mask:true
      })
      wx.request({
          url: 'http://43.139.97.111:8080/newBooks/queryByPage?pageNum=' + page + '&pageSize=4',       
          header: {
            'content-type': 'application/json' // 默认值
          },
          success (res) {
            console.log(res.data);
            if(res.statusCode==200){
              _this.setData({
                newbooks:res.data.data.list,
              });
            }
          wx.hideLoading()
        }
      })
    },

    getAllbooks: function () {
      var _this=this;
      var i = util.integer(Math.random() * 16 + 1)   //i的取值范围为[0,16]的小数,[]是包含等于 (不好含等于
      console.log(i);
      let page = i;
      wx.showLoading({
        title: '加载中',
        mask:true
      })
      wx.request({
          url: 'http://43.139.97.111:8080/books/queryByPage?pageNum=' + page + '&pageSize=10',       
          header: {
            'content-type': 'application/json' // 默认值
          },
          success (res) {
            console.log(res.data);
            if(res.statusCode==200){
              _this.setData({
                allbooks:res.data.data.list,
              });
            }
          wx.hideLoading()
        }
      })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {
      let that = this;
      let page = that.data.page; //获取当前的页数
      wx.request({
          url: 'http://43.139.97.111:8080/newBooks/queryByPage?pageNum=&pageSize=4',
          header: {
              'content-type': 'application/json'
          },
          data: {
            pageNum: page + 1
          },
          success(res) {
              let resbook = res.data.data.list;
              that.setData({
                  newbook: resbook,
                  page: page,
              })
              if (resbook.length > 0) {
                  wx.showToast({
                      title: '刷新成功',
                      icon: 'success'
                  })
              }
          }
      })
  },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {
      console.log("上拉了");
      let that = this;
      let page = this.data.page;
      wx.request({
        url: 'http://43.139.97.111:8080/newBooks/queryByPage?pageNum=&pageSize=4',
        header: {
            'content-type': 'application/json' // 默认值
        },
        data: {
          pageNum: page + 1,
        },
        success(res) {
          console.log(res.data.data);
          let resbook = res.data.data.list;
          that.setData({
            newbooks: that.data.newbooks.concat(resbook),
            page: page + 1,
          })

          if (resbook.length > 0) {
            wx.showToast({
              title: '加载数据...',
              icon: 'loading'
            })
          } else {
            wx.showToast({
              title: '暂无更多...',
              icon: 'none'
            })
          }
        }
      })
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    },

    skipTravelDetails:function(e){
      let id=e.currentTarget.dataset.id 
      wx.navigateTo({
          url: "/pages/bookDetail/bookDetail?id=" + id,
      })
    },

    skipCouponsDetails:function(e){
      wx.navigateTo({
          url: "/pages/couponsDetail/couponsDetail",
      })
    },

    closePupCoupon: function () {
      this.setData({
        isSuccess: false
      })
    },
})